<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/4/12
 * Time: 17:46
 */

namespace app\model\production;


use App\Common\Lib\Common;
use App\Exceptions\UserException;
use App\Model\BaseModel;
use App\Model\Category\Category;
use App\Model\Customer\UserCollectModel;
use App\Model\Invented\InventedUserModel;
use App\Model\Widget\WidgetModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class ProductionAttrModel extends BaseModel
{
    protected $table = 'ting_production_attr';

    protected $primaryKey = 'sale_attr_id';

    const CREATED_AT = 'createtime';
    const UPDATED_AT = 'updatetime';

    protected static $instance;

    public static function getInstance()
    {
        if(is_object(self::$instance)){
            return self::$instance;
        }

        self::$instance = new self();

        return self::$instance;
    }

    public function index($where, $field=[],$order = [])
    {
        $query =  $this->getWhere($where);

        $field && $query->select($field);

        $data = $this->buildPage($query);

        $order && $query->orderBy($order[0],$order[1]);

        $data['data'] = $query->get();

        return $data;
    }

    public function getList($param)
    {
        $where = [
            'isdel'=>0,
            'status'=>1
        ];

        $query = $this->where($where);
        $data['data'] = $query->orderBy('createtime','desc')->get();

        return $data;
    }

    public function getDetail($where,$field=[])
    {
        $query = $this->getWhere($where);
        $field && $detail = $query->select($field);
        $detail =  $query->first();

        if($detail==false){
            throw new UserException(4040);
        }

        return $detail;
    }
}